package com.mall.common.controller;

import static com.mall.common.Constants.ROLE_SEPARATOR;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;

import com.mall.common.bean.User;
import com.mall.common.service.OperateService;

public class BaseAuthController extends BaseController {
	@Resource private OperateService operateService;

	/**
	 * 按钮权限判断
	 * @param buttonInfo 按钮信息（key：model的键值，value：按钮的请求路径）
	 * @param model
	 */
	protected void buttonAuth(String key, String url, Model model){
		User user = getUser();
		if(user.isSystemManager()){
			return;
		}
		List<String> roleList = user.getRoleListValue();
		Map<String, String> urlRoles = operateService.getAllUrlRole();
		String grantRoleStr = urlRoles.get(url);
		if(StringUtils.isNotBlank(grantRoleStr)){
			boolean flag = false;
			String[] roles = grantRoleStr.split(ROLE_SEPARATOR);
			for(String role : roles){
				if(roleList.contains(role)){
					flag = true;
					break;
				}
			}
			model.addAttribute(key, flag);
		}
	}
}
